#pragma once
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<iostream>
using namespace std;
class InetAddr
{
private:
    void ToHost()
    {
        _port = ntohs(_addr.sin_port);
        //_ip = inet_ntoa(_addr.sin_addr);//但是这个不是线程安全程度
        char ip[32];
        ::inet_ntop(AF_INET, &_addr.sin_addr, ip, sizeof(ip));
        _ip = ip;
    }

public:
    InetAddr(const struct sockaddr_in& addr):_addr(addr)
    {
        ToHost();
    }
    string Ip() { return _ip; }
    uint16_t Port() { return _port; }
    struct sockaddr_in Addr()
    {
        return _addr;
    }
    bool operator ==(const InetAddr& addr)
    {
        //运行一个ip可以开多个客户端
        if(addr._ip==_ip&&addr._port==_port)
        {
            return true;
        }
        return false;
    }

private:
    string _ip;
    uint16_t _port;
    struct sockaddr_in _addr;
};
